package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Rodgers
 * @Date 2022/3/14 16:30
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    //处理添加套餐请求
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        //3.调用业务实现添加套餐
        setmealService.save(setmealDto,session);

        //4.封装数据返回
        return R.success("添加套餐成功");
    }

    /**
     * 分页查询套餐
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> findByPage(Integer page,Integer pageSize,String name){
        Page<SetmealDto> dtoPage = setmealService.findByPage(page, pageSize, name);

        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @param session
     * @return
     */
    public R<String> deleteByIds(@RequestParam List<Long> ids,HttpSession session){
        Long id = (Long) session.getAttribute("employee");

        setmealService.deleteByIds(ids,id);

        return R.success("删除套餐成功");
    }

}
